﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.PregnantCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.PregnantCommandsHandlers
{
    /// <summary>
    /// 
    /// </summary>
    public class PregnantUpdateCommandHandler : IRequestHandler<PregnantUpdateCommand, int>
    {
        private readonly IRepository<Pregnant> repository;
        private readonly IMapper mapper;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="mapper"></param>
        public PregnantUpdateCommandHandler(IRepository<Pregnant> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(PregnantUpdateCommand request, CancellationToken cancellationToken)
        {
            var pregnant = new Pregnant();
            request.UpdateDate = DateTime.Now;
            return await repository.UpdateAsync(mapper.Map<PregnantUpdateCommand, Pregnant>(request, pregnant));
        }
    }
}
